package net.fpeg.kotlin.msa.wordbase.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import net.fpeg.kotlin.msa.wordbase.service.WordSourceService
import org.springframework.beans.factory.annotation.Autowired
import javax.persistence.*


@Entity
@Table(name = "word_source")
class WordSource(
        @JsonProperty("title") var value: String,
        @ManyToOne
        @JoinColumn(name = "word_source_parent_id")
        @JsonIgnore
        var wordSourceParent: WordSource?,
        @ManyToOne
        @JsonIgnore
        @JoinColumn(name = "word_user_id")
        var wordUser: WordUser
) {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("key")
    var id: Long? = null

    @OneToMany(mappedBy = "wordSourceParent", fetch = FetchType.EAGER, orphanRemoval = true)
    @OrderBy("value ASC")
    @JsonProperty("children")
    lateinit var wordSourceChildren: Set<WordSource>


    @JsonIgnore
    @OneToMany(mappedBy = "wordSource", cascade = [CascadeType.DETACH])
    lateinit var wordSentences: Set<WordSentence>


//    @PreRemove
//    private fun removeAssociationsWithChilds() {
//        for (wordSentence in wordSentences) {
//            wordSentence.wordSource =
//        }
//    }

}